/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.grnt.common;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.sql.DataSource;
import org.grnt.common.ContextWraper.ContextRewrapException;
import org.grnt.dbcommon.DataControler.DataControlException;
import org.grnt.dbcommon.DataControler;

/**
 *
 * It is servlet in the system which be run first when service started. It initializes
 * ContextWraper object and DataControler object.
 * @author sayid
 */
public class FirstSevlet extends HttpServlet {

	/**
	 *
	 * In this servlet init, ContextWraper and DataControler will be inited.
	 * @throws ServletException
	 */
	@Override
	public void init() throws ServletException {
		try {
			super.init();
			if (!ContextWraper.isActive())
				ContextWraper.wrap(getServletContext());
			if (!DataControler.isActive()) {
				DataSource datasource = (DataSource) getServletContext().getAttribute("DBCPool");
				DataControler.registDataSource(datasource);
			}
		} catch (ContextRewrapException ex) {
			throw new ServletException(ex);
		} catch (DataControlException e) {
			throw new ServletException(e);
		}
	}
}
